package ca.scotthyndman.as2haxe.ui;

import java.awt.BorderLayout;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JList;
import java.awt.GridBagLayout;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.filechooser.FileFilter;

import java.awt.FlowLayout;
import java.io.File;

public class MainForm extends JFrame {

	private static final long serialVersionUID = 1L;

	private FileFilter directoryFilter = new FileFilter() {

		@Override
		public boolean accept(File f) {
			return f.isDirectory();
		}

		@Override
		public String getDescription() {
			return "Directories";
		}

	};

	private JPanel jContentPane = null;

	private JPanel stdLibPanel = null;

	private JTextField stdPath = null;

	private JLabel stdLbl = null;

	private JToggleButton browseBtn = null;

	private JPanel cpPanel = null;

	private JList cpList = null;

	private JPanel buttonPanel = null;

	private JToggleButton addCp = null;

	private JToggleButton removeCp = null;

	/**
	 * This is the default constructor
	 */
	public MainForm() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(420, 211);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getStdLibPanel(), BorderLayout.NORTH);
			jContentPane.add(getCpPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes stdLibPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getStdLibPanel() {
		if (stdLibPanel == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(3);
			borderLayout.setVgap(2);
			stdLbl = new JLabel();
			stdLbl.setText("Flash Classes:");
			stdLbl.setPreferredSize(new Dimension(96, 16));
			stdLibPanel = new JPanel();
			stdLibPanel.setLayout(borderLayout);
			stdLibPanel.setPreferredSize(new Dimension(0, 24));
			stdLibPanel.add(getStdPath(), BorderLayout.CENTER);
			stdLibPanel.add(stdLbl, BorderLayout.WEST);
			stdLibPanel.add(getBrowseBtn(), BorderLayout.EAST);
		}
		return stdLibPanel;
	}

	/**
	 * This method initializes stdPath
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getStdPath() {
		if (stdPath == null) {
			stdPath = new JTextField();
			stdPath.setEditable(false);
			stdPath.setPreferredSize(new Dimension(8, 20));
		}
		return stdPath;
	}

	/**
	 * This method initializes browseBtn
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getBrowseBtn() {
		if (browseBtn == null) {
			browseBtn = new JToggleButton();
			browseBtn.setText("Browse...");
			browseBtn.setPreferredSize(new Dimension(84, 20));
			browseBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser chooser = new JFileChooser();
					chooser.setFileFilter(MainForm.this.directoryFilter);
					int res = chooser.showOpenDialog(MainForm.this);

					if (res == JFileChooser.APPROVE_OPTION) {
						MainForm.this.stdPath.setText(chooser
								.getSelectedFile().getAbsolutePath());
					}
				}
			});
		}
		return browseBtn;
	}

	/**
	 * This method initializes cpPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getCpPanel() {
		if (cpPanel == null) {
			cpPanel = new JPanel();
			cpPanel.setLayout(new BorderLayout());
			cpPanel.add(getCpList(), BorderLayout.CENTER);
			cpPanel.add(getButtonPanel(), BorderLayout.SOUTH);
		}
		return cpPanel;
	}

	/**
	 * This method initializes cpList
	 * 
	 * @return javax.swing.JList
	 */
	private JList getCpList() {
		if (cpList == null) {
			cpList = new JList();
			cpList.setPreferredSize(new Dimension(0, 40));
			cpList.setVisibleRowCount(10);
		}
		return cpList;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.RIGHT);
			flowLayout.setVgap(2);
			flowLayout.setAlignment(FlowLayout.RIGHT);
			flowLayout.setVgap(2);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(flowLayout);
			buttonPanel.setPreferredSize(new Dimension(0, 24));
			buttonPanel.add(getAddCp(), null);
			buttonPanel.add(getRemoveCp(), null);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes addCp
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getAddCp() {
		if (addCp == null) {
			addCp = new JToggleButton();
			addCp.setPreferredSize(new Dimension(70, 20));
			addCp.setText("Add...");
			addCp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser chooser = new JFileChooser();
					chooser.setFileFilter(MainForm.this.directoryFilter);
					int res = chooser.showOpenDialog(MainForm.this);
					
					if (res == JFileChooser.APPROVE_OPTION) {
						
					}
				}
			});
		}
		return addCp;
	}

	/**
	 * This method initializes removeCp
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JToggleButton getRemoveCp() {
		if (removeCp == null) {
			removeCp = new JToggleButton();
			removeCp.setPreferredSize(new Dimension(85, 20));
			removeCp.setText("Remove");
		}
		return removeCp;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
